function [ subjectinfo ] = analyzeVisualResponsivenessFull_Calculate( subjectinfo )
%ANALYZEVISUALRESPONSIVENESS Analyzes the data for visual responsiveness

% STEP 2
frequenciesOfInterest = 4:0.2:100;
timeSteps = 0.01;
% frequenciesOfInterest = 4:2:100;
% timeSteps = 1;
% frequenciesOfInterest = 5:20:95; % For quick testing..
% timeSteps = 1; % For quick testing..
subjectinfo = Step2(subjectinfo, frequenciesOfInterest, timeSteps);

subjectinfo.Save;

end

%% Step 2
function [s] = Step2(s, frequenciesOfInterest, timeSteps)
% Process each set
for x = 1:length(s.Analyses.VisualResponsiveness.Sets) % TODO: Make this a parfor!    
    % Load the data and convert to fieldtrip structure
    EEG = pop_loadset( 'filename', s.Analyses.VisualResponsiveness.Sets(x).DataFilenameRest);
    dataRest = eeglab2fieldtrip(EEG, 'preprocessing');
    EEG = pop_loadset( 'filename', s.Analyses.VisualResponsiveness.Sets(x).DataFilenameStimulus);
    dataStimulus = eeglab2fieldtrip(EEG, 'preprocessing');
    
    electrodeLabels = dataRest.label;
    clear EEG;
    
    cfg = [];
    cfg.channel    = 'all';
    cfg.method     = 'wltconvol';
    cfg.output     = 'pow';
    cfg.keeptrials = 'no'; % will average all epochs..
    cfg.foi        = frequenciesOfInterest;
    
    cfg.toi        = 1:timeSteps:max(dataRest.time{1});
    waveRest = freqanalysis(cfg, dataRest);
    clear dataRest;
    
    cfg.toi        = 1:timeSteps:max(dataStimulus.time{1});
    waveStimulus = freqanalysis(cfg, dataStimulus);
    clear dataStimulus;
    
    % Save the calculated wave sprectra
    s.Analyses.VisualResponsiveness.Sets(x).WavedataFilename = ...
        [s.Analyses.VisualResponsiveness.Filepath s.PrimaryPrefix ' Wavedata for VisResp Set ' num2str(x) '.mat'];
    save('-V7.3', s.Analyses.VisualResponsiveness.Sets(x).WavedataFilename, 'waveRest', 'waveStimulus', 'frequenciesOfInterest', 'electrodeLabels');
end
end